//
//  FindPWDViewController.m
//  BasketballClub
//
//  Created by 孙鸿吉 on 16/1/14.
//  Copyright © 2016年 孙鸿吉. All rights reserved.
//

#import "FindPWDViewController.h"
#import "FindPWDView.h"
#import <AVOSCloud.h>
@interface FindPWDViewController ()<UITextFieldDelegate>
@property (nonatomic,strong)FindPWDView *findPWDView;
@end

@implementation FindPWDViewController
- (void)loadView
{
    self.findPWDView = [[FindPWDView alloc]initWithFrame:[UIScreen mainScreen].bounds];
    self.view = self.findPWDView;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.findPWDView.backgroundColor = ColorHex(0x0d1c38);
    //返回按钮方法
    [self.findPWDView.cancleButton addTarget:self action:@selector(returnBeforeAction) forControlEvents:UIControlEventTouchUpInside];
    //发送按钮方法
    [self.findPWDView.sendEmailButton addTarget:self action:@selector(sendEmailAction) forControlEvents:UIControlEventTouchUpInside];
}


#pragma mark - 回收键盘
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [self.findPWDView.emailTF resignFirstResponder];
    return YES;
}
//点击空白处回收键盘方法
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}

#pragma mark - 返回按钮
- (void)returnBeforeAction
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark - 发送按钮方法
- (void)sendEmailAction
{
    [AVUser requestPasswordResetForEmailInBackground:self.findPWDView.emailTF.text block:^(BOOL succeeded, NSError *error) {
        if (succeeded) {
            //邮件发送成功 弹出提示框告诉发送成功
            [self emailSendSuccess];
            [self dismissViewControllerAnimated:YES completion:nil];
        }else{
            
        }
    }];
    
}


#pragma mark - 提示框(邮件发送成功)
- (void)emailSendSuccess
{
    CGFloat verison = [[[UIDevice currentDevice]systemVersion]floatValue];
    if (verison >= 8.0) {
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"邮件发送成功" message:nil preferredStyle:UIAlertControllerStyleAlert];
        [self presentViewController:alert animated:YES completion:nil];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self dismissViewControllerAnimated:YES completion:nil];
        });
        
    }else{
        UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"邮件发送成功" message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:nil];
        [alertView show];
        
        [self performSelector:@selector(aftertimeAction:) withObject:alertView afterDelay:2.0];
    }
    
}

#pragma mark - 延迟2秒方法
- (void)aftertimeAction:(UIAlertView *)sender
{
    [sender dismissWithClickedButtonIndex:0 animated:YES];
    
}









- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
